#ifndef __SARADC_H__
#define __SARADC_H__

#include "io.h"
#include "bits.h"
#include "mem_map_table.h"
#include <stdbool.h>

#ifdef __cplusplus
extern "C" {
#endif

#define SARADC_REG_BASE                      MEM_MAP_SARADC_BASE_ADDR
#define SARADC                               ((SARADC_REG_PTR)SARADC_REG_BASE)

#define ADC_CTRL0_CHAN_EN_MASK               GENMASK(4, 0)
#define ADC_CTRL0_CHAN_EN_POS                0
#define ADC_CTRL0_BAIS_DISABLE               BIT(24)
#define ADC_CTRL0_PD_DISABLE                 BIT(20)
#define SAMP_CTRL_SAMP_TIME_MASK             GENMASK(2, 0)
#define SAMP_CTRL_SAMP_TIME_POS              0
#define SAMP_CTRL_SWITCH_TIME_MASK           GENMASK(23, 16)
#define SAMP_CTRL_SWITCH_TIME_POS            16
#define ADC_CHAN_CALI_B_EN_MASK              BIT(28)
#define ADC_CHAN_CALI_B_PARA_MASK            GENMASK(27, 16)
#define ADC_CHAN_CALI_B_PARA_POS             16
#define ADC_CHAN_CALI_K_EN_MASK              BIT(12)
#define ADC_CHAN_CALI_K_PARA_MASK            GENMASK(11, 0)
#define ADC_CHAN_CALI_K_PARA_POS             0
#define ADC_CTRL1_TEMP_CHAN_CFG_MASK         GENMASK(23, 20)
#define ADC_CTRL1_TEMP_CHAN_CFG_POS          20
#define ADC_CTRL1_LEAK_CHAN_CFG_MASK         GENMASK(19, 16)
#define ADC_CTRL1_LEAK_CHAN_CFG_POS          16
#define ADC_CTRL1_LEAK_SAMP_RATE_MASK        GENMASK(15, 0)
#define ADC_CTRL1_LEAK_SAMP_RATE_POS         0
#define ADC_CTRL2_ADC_ENBALE                 BIT(0)

#define ADC_SAMP_RATE_3200                   (6144UL)
#define ADC_SAMP_RATE_6400                   (3072UL)
#define ADC_SAMP_RATE_9600                   (2048UL)
#define ADC_SAMP_RATE_32000                  (614UL)
#define ADC_SAMP_RATE_204800                 (96UL)

#define ADC_DEFAULT_CHAN_NUMS                (13UL)
#define ADC_DEFAULT_SAMP_TIME                (0UL)
#define ADC_DEFAULT_SWITCH_TIME              (7UL)

typedef enum {
	SARADC_CHAN0  = 0,
	ARC1_CHAN     = 0,
	SARADC_CHAN1  = 1,
	ARC2_CHAN     = 1,
	SARADC_CHAN2  = 2,
	ARC3_CHAN     = 2,
	SARADC_CHAN3,
	SARADC_CHAN4,
	SARADC_CHAN5,
	SARADC_CHAN6,
	SARADC_CHAN7,
	SARADC_CHAN8,
	SARADC_CHAN9,
	SARADC_CHAN10,
	SARADC_CHAN11 = 11,
	LEAK_CHAN     = 11,
	SARADC_CHAN12 = 12,
	TEMP_CHAN     = 12,
	SARADC_CHAN13,
	SARADC_CHAN14,
	SARADC_CHAN15,
	SARADC_CHAN_MAX,
} saradc_chan;

typedef enum {
	EXT0_MAP_CHAN,
	EXT1_MAP_CHAN,
	EXT2_MAP_CHAN,
	EXT3_MAP_CHAN,
	EXT4_MAP_CHAN,
	EXT5_MAP_CHAN,
	EXT6_MAP_CHAN,
	EXT7_MAP_CHAN,
	EXT8_MAP_CHAN,
	EXT9_MAP_CHAN,
	EXT10_MAP_CHAN,
	ARC1_MAP_CHAN,
	ARC2_MAP_CHAN,
	ARC3_MAP_CHAN,
	MAP_CHAN_MAX,
} saradc_map_chan;

typedef struct saradc_reg {
	__IOM uint32_t ADC_CTRL0;                     /*0x00*/
	__IOM uint32_t SAMP_CTRL;                     /*0x04*/
	__IOM uint32_t CALI_CTRL[16];                 /*0x08*/
	__IOM uint32_t CHAN_MAP_CTRL[2];              /*0x48*/
	__IOM uint32_t ADC_CTRL1;                     /*0x50*/
	__IOM uint32_t TEMP_THRES_CTRL;               /*0x54*/
	__IOM uint32_t SAMP_RATE_CTRL[16];            /*0x58*/
	__IM  uint32_t SAMP_DATA[6];                  /*0x98*/
	__OM  uint32_t ADC_CTRL2;                     /*0xb0*/
	__OM  uint32_t INT_CTRL;                      /*0xb4*/
	__OM  uint32_t INT_CLR;                       /*0xb8*/
	__IOM uint32_t INT_MASK;                      /*0xbc*/
	__IM  uint32_t INT_STA;                       /*0xc0*/
} SARADC_REG, *SARADC_REG_PTR;

typedef void (*saradc_int_callback)(void);

int32_t saradc_chan_cali_cfg(saradc_chan chan, uint32_t b_para, uint32_t k_para);
void saradc_temp_threshold_cfg(uint32_t value);
int32_t saradc_temp_chan_cfg(saradc_chan chan);
int32_t saradc_leak_chan_cfg(saradc_chan chan);
void saradc_leak_samp_rate_cfg(uint32_t samp_rate);
int32_t saradc_chan_samp_rate_cfg(saradc_chan chan, uint32_t sample_rate);
void saradc_bais_power_en(bool enable);
void saradc_analog_power_en(bool enable);
int32_t saradc_chans_enable(uint8_t chan_nums);
void saradc_en(bool enable);
int32_t saradc_chan_map(saradc_map_chan map_chan, saradc_chan chan);
void saradc_default_init(void);
void saradc_en_int_callback(saradc_int_callback cb);

#ifdef __cplusplus
}
#endif

#endif
